package com.scala.zhenghui

import scala.util.control.Breaks._
import scala.collection.mutable.ArrayBuffer

object firstone {
  def main(args: Array[String]) {
    //   processControl()
    //   welcomeGameHouse()
    //   println(sayHello("zhenghui", 30))
    //   sayHello("eric")
    //   println(sum(100))
    //   println(fab(10))//
    //  sayDefaultParm("hui")
    // sayDefaultParm(middleName="hui",firtName="gogo")
    // println(sum(1, 2, 3, 4))

    //println(sum(1 to 10: _*))
    //println(sum2(1 to 10: _*))
    // writeArray()
    writeArrayBuffer()
  }
  //数组
  def writeArray() {
    var arr = new Array[String](10)

    for (i <- 0 until arr.length)
      arr(i) = System.currentTimeMillis() + ""
    for (i <- 0 until arr.length)
      println(arr(i))
  }
  //数组
  def writeArrayBuffer() {
    var arr = new ArrayBuffer[Int]()
    arr += 1
    arr += (1, 2, 3, 4, 5)
    arr.insert(1, 2, 3, 4, 5)
    arr.insert(1, 9)
    arr.remove(0, 2)
    arr.remove(0)

    for (i <- 0 until arr.length)
      print(arr(i))
    println()
    var array = arr.toArray
    for (i <- 0 until array.length)
      print(array(i))
    println()
    for (i <- 0 until (array.length, 2))
      println(i)
    println()
    for (i <- (0 until array.length).reverse)
      println(array(i))
      
        println()
    for (i <- array)
      println(i)
      
      println(array.mkString("<","#",">"))
  }
  //默认参数
  def sayDefaultParm(firtName: String, middleName: String = "Ming", lastName: String = "Zheng") {
    println("hello " + firtName + " " + middleName + " " + lastName)
  }

  //变长参数
  def sum(nums: Int*) = {
    var res = 0
    for (num <- nums)
      res += num
    res
  }

  //变长参数递归
  def sum2(nums: Int*): Int = {
    var res = 0
    if (nums.length == 0) 0
    else nums.head + sum2(nums.tail: _*)
  }

  //递归函数
  def fab(n: Int): Int = {
    if (n <= 0) 1
    else
      fab(n - 1) + fab(n - 2)
  }

  //函数
  def sayHello(name: String, age: Int) = {
    if (age > 18)
      printf("hi %s,you are a big boy!!!\n", name)
    else
      printf("hi %s,you are a children!!!\n", name)
    age
  }
  //单行函数
  def sayHello(name: String) = println("hello " + name)

  def sum(n: Int) = {
    var result = 0
    for (i <- 1 to n)
      result += i
    result

  }

  def processControl() {
    println("Hello, Scala")
    var age = 30
    var result = if (age > 18)
      "audit"
    else
      "children"
    print(result)
    var name = "eric"
    printf("Hello, Scala,I am %s", name)
    println()
    var n = 10
    while (n > 0) {
      println(n + "")
      n -= 1
    }
    //for循环 1
    n = 10
    for (i <- 1 to n)
      print(i + " ")
    println()
    //忽略末尾
    for (i <- 1 until n)
      print(i + " ")
    println()
    //增强for循环
    for (i <- "Hello World")
      print(i + " ")
    println()
    //break
    breakable {
      var n = 10
      for (c <- "Hello World") {
        if (n == 5) break
        print(c)
        n -= 1
      }
    }

    println()
    //多重FOR循环
    for (i <- 1 to 9; j <- 1 to 9) {
      if (j == 9) {
        println(i * j)
      } else {
        print(i * j + " ")
      }

    }
    println()

    for (i <- 1 to 20 if i % 2 == 1) {
      print(i + " ")
    }
    println()
    //推导式for循环 构造集合
    var array = for (i <- 1 to 10) yield i
    print(array)
  }

  def welcomeGameHouse() {

    val name = readLine("welcome to Game House.Please tell me your name:")

    println("Thanks.Then please tell me your age:")

    val age = readInt()
    if (age > 18)
      printf("HI,%S,You are %d years old ,so you are legel to come here!", name, age)
    else
      printf("Sorry,%S,You are only %d years old ,so you are illegel to come here!", name, age)

  }
}